<?php
/**
 * Created by PhpStorm.
 * User: Dinglf
 * Date: 2016/11/24 0024
 * Time: 21:46
 */
namespace app\admin\controller;

use app\admin\model\Zombie;
use think\Controller;
use think\Curler;
use preg\PregMatch;
use app\admin\model\User;
use think\Request;

class WeiBo extends Controller {
    public static $CONFIG = [];

    public function __construct(Request $request)
    {
        parent::__construct($request);
        self::$CONFIG['log_file'] = date('Ymd') . '.log';
    }

    public function index()
    {
        $log_file = date('Ymd') . '.log';
        ignore_user_abort();
        $id = 0;
        $exit = true;
        set_time_limit(0);
        while ($exit)
        {   
            $listName = Zombie::getUserName();
            if (empty($listName))
            {
                file_put_contents(self::$CONFIG['log_file'], date('Y-m-d H:i:s') . 'SUCCESS' . PHP_EOL, FILE_APPEND);
            }
            //$listName[0]['u_name'] = 
            foreach ($listName as $key => $value) {
                $searchName = $listName[$key]['u_name'];
                if (!PregMatch::searchUser($searchName))
                {
                    if (Zombie::backError($listName[$key]['id']))
                    {
                        file_put_contents(self::$CONFIG['log_file'], date('Y-m-d H:i:s') . ':用户' . $searchName . '回退成功' . '用户id为' . $listName[$key]['id'] . PHP_EOL, FILE_APPEND);
                    }
                    else
                    {
                        file_put_contents(self::$CONFIG['log_file'], date('Y-m-d H:i:s') . ':用户' . $searchName . '回退失败，请手动回退' . '用户id为' . $listName[$key]['id'] . PHP_EOL, FILE_APPEND);
                    }
                    echo date('Y-m-d H:i:s') . ':用户' . $searchName . '的信息已抓取失败。用户id为' . $listName[$key]['id'];
                    
                }
                else 
                {
                    $user = PregMatch::getUserInfo($searchName);
                    if (Zombie::UpdateUser($listName[$key]['id'], $user))
                    {
                        file_put_contents(self::$CONFIG['log_file'], date('Y-m-d H:i:s') . ':用户' . $searchName . '信息写入成功' . '用户id为' . $listName[$key]['id'] . PHP_EOL, FILE_APPEND);
                    }
                    else
                    {
                        file_put_contents(self::$CONFIG['log_file'], date('Y-m-d H:i:s') . ':用户' . $searchName . '信息写入失败' . '用户id为' . $listName[$key]['id'] . PHP_EOL, FILE_APPEND);
                    }
                    echo date('Y-m-d H:i:s') . ':用户' . $searchName . '的信息已抓取完成。用户id为' . $listName[$key]['id'];
                }
                $id += 10;
                if ($id > 452 )
                {
                    $exit = false;
                }
            }  
            sleep(20);
        }
    }
    public function test()
    {
        echo 'test';
    }
}